package com.ipay.wechat.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

public class MessageUtils {
	public static String printXml(String msgType, String content,
			String fromUserName, String toUserName, HttpServletResponse response) {
	    try {
			PrintWriter out=response.getWriter();
			if("text".equals(msgType)){
				 printText(msgType,content,fromUserName,toUserName,out);
			}
			out.flush();
			out.close();
			out=null;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static void printText(String msgType, String content,
			String fromUserName, String toUserName, PrintWriter out) {
		out.print("<xml>");
		out.print("<ToUserName><![CDATA["+fromUserName+"]]></ToUserName>");
		out.print("<FromUserName><![CDATA["+toUserName+"]]></FromUserName>");
		out.print("<CreateTime>"+String.valueOf(new Date().getTime()/1000)+"</CreateTime>");
		out.print("<MsgType><![CDATA["+msgType+"]]></MsgType>");
		out.print("<Content><![CDATA["+content+"]]></Content>");
		out.print("</xml>");
	}
}
